/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.commands.JSCommand;
import fever.commands.JSMethodName;
import fever.data.validators.ValidatorStrategy;
import fever.utils.Stringifier;

/**
 * Email address validation using <strong>Javascript RegExp</strong>. 
 *
 * @author Romain Ecarnot
 */
class fever.data.validators.email.JSEmailStrategy implements ValidatorStrategy
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : JSEmailStrategy;
	private var _command : JSCommand;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns {@link JSEmailStrategy} instance.
	 * 
	 * <p>Always returns the same instance.<br />
	 * Design Pattern Singleton.
	 * @return {@link JSEmailStrategy} instance
	 */
	public static function getInstance() : JSEmailStrategy
	{
		if( !_instance ) _instance = new JSEmailStrategy();
		return _instance;	
	}
	
	/**
	 * Returns {@code true} if passed-in {@code email} adress is correct, 
	 * otherwise {@code false}.
	 */
	public function validate( email : String ) : Boolean
	{
		return Boolean( _command.call( email ) );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function JSEmailStrategy()
	{
		var f : String = "";
		f += "( email )";
		f += "{";
		f += "var pattern = /^([a-zA-Z0-9_\\\\\\.\\\\\\-])+\\\\\\@(([a-zA-Z0-9\\\\\\-])+\\\\\\.)+([a-zA-Z0-9]{2,4})+$/;";
		f += "var matcher = new RegExp( pattern );";
		f += "return matcher.test( email );";
		f += "}";
			
		_command = new JSCommand( new JSMethodName( 'validEmail' ), f );
	}
	
}